# -*- coding:utf-8 -*-
"""
海龟交易
"""
from function.interface import *
import talib as ta

class Strategy6(JyInterface):
    symbol = 'XRP/USDT'
    period = '1min'
    size = 2000
    donchianNum = 20
    atrLength = 20
    cash = 1000
    priceLastTrade = 0
    def __init__(self, **kwargs):
        super(Strategy6, self).__init__(**kwargs)
        self.balance = self.cash
    def loop(self):
        """策略部分"""
        kline = self.getKline(self.symbol, self.period, self.size)
        donchianHigh = max(kline['highs'][self.donchianNum-1:-1])
        donchianLow = min(kline['lows'][self.donchianNum-1:-1])
        atr = ta.ATR(np.array(kline['highs']), np.array(kline['lows']), np.array(kline['closes']), timeperiod=self.atrLength)
        n = atr[-1]
        lastprice = self.getLastPrice(self.symbol)
        unit = min((self.cash * 0.01)/n/lastprice/100, (self.cash * 0.01)/lastprice, self.balance/lastprice)
        position = self.getPositionSt(self.symbol)
        if position == 0:
            if lastprice > donchianHigh:
                orderId = self.buy(self.symbol, lastprice, unit)
                self.balance = self.balance - lastprice * unit
                self.priceLastTrade = lastprice
            elif self.futureOrSpot == 1 and lastprice < donchianLow:
                orderId = self.short(self.symbol, lastprice, unit)
                self.balance = self.balance - lastprice * unit
                self.priceLastTrade = lastprice
        elif position > 0:
            if lastprice > self.priceLastTrade + 0.5*n:
                orderId = self.buy(self.symbol, lastprice, unit)
                self.balance = self.balance - lastprice * unit                
                self.priceLastTrade = lastprice
            elif lastprice < self.priceLastTrade - 2*n:
                orderId = self.sell(self.symbol, lastprice, position)
                self.balance = self.balance + lastprice * unit
            elif lastprice < donchianLow:
                orderId = self.sell(self.symbol, lastprice, position)
                self.balance = self.balance + lastprice * unit                
        elif position < 0:
            if lastprice < self.priceLastTrade - 0.5*n:
                orderId = self.short(self.symbol, lastprice, unit)
                self.balance = self.balance - lastprice * unit # 做空的额度减少                
                self.priceLastTrade = lastprice
            elif lastprice > self.priceLastTrade + 2*n:
                orderId = self.cover(self.symbol, lastprice, position)
                self.balance = self.balance + lastprice * unit    
            elif lastprice > donchianHigh:
                orderId = self.cover(self.symbol, lastprice, position)
                self.balance = self.balance + lastprice * unit

if __name__ == "__main__":
    st = Strategy6(type_=1,futureOrSpot=0,appKey='e1165-78937',secret='bf36b118-5',passphrase='',transferId='98_41',grpcAddress="18.136.163.117:9309")
    st.run(st.loop)